home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Peter's Final Project / sys / timer.c < prev    next >
Text File  |  1995-05-10  |  2KB  |  117 lines

  1. /*
  2.  *  Peter's Final Project -- A texture mapping demonstration
  3.  *  © 1995, Peter Mattis
  4.  *
  5.  *  E-mail:
  6.  *  petm@soda.csua.berkeley.edu
  7.  *
  8.  *  Snail-mail:
  9.  *   Peter Mattis
  10.  *   557 Fort Laramie Dr.
  11.  *   Sunnyvale, CA 94087
  12.  *
  13.  *  Avaible from:
  14.  *  http://www.csua.berkeley.edu/~petm/final.html
  15.  *
  16.  *  This program is free software; you can redistribute it and/or modify
  17.  *  it under the terms of the GNU General Public License as published by
  18.  *  the Free Software Foundation; either version 2 of the License, or
  19.  *  (at your option) any later version.
  20.  *
  21.  *  This program is distributed in the hope that it will be useful,
  22.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  *  GNU General Public License for more details.
  25.  *
  26.  *  You should have received a copy of the GNU General Public License
  27.  *  along with this program; if not, write to the Free Software
  28.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  */
  30.  
  31. #include <stdlib.h>
  32. #include <Events.h>
  33.  
  34. #include "sys.stuff.h"
  35. #include "timer.h"
  36.  
  37. /*
  38.  * Make a timer object.
  39.  */
  40.  
  41. TIMER
  42. make_timer ()
  43. {
  44.     TIMER timer;
  45.     
  46.     timer = (TIMER) ALLOC (sizeof (struct _TIMER));
  47.     timer->start = TickCount ();
  48.     
  49.     return timer;
  50. }
  51.  
  52. /*
  53.  * Free a timer object.
  54.  */
  55.  
  56. void
  57. free_timer (t)
  58.     TIMER t;
  59. {
  60.     FREE (t);
  61. }
  62.  
  63. /*
  64.  * Start the timer.
  65.  */
  66.  
  67. void
  68. timer_start (t)
  69.     TIMER t;
  70. {
  71.     t->activated = 1;
  72.     t->start = TickCount ();
  73. }
  74.  
  75. /*
  76.  * Stop the timer.
  77.  */
  78.  
  79. void
  80. timer_stop (t)
  81.     TIMER t;
  82. {
  83.     t->activated = 0;
  84.     t->end = TickCount ();
  85. }
  86.  
  87. /*
  88.  * Reset the timer.
  89.  */
  90.  
  91. void
  92. timer_reset (t)
  93.     TIMER t;
  94. {
  95.     timer_start (t);
  96. }
  97.  
  98. /*
  99.  * Return the time elapsed since the timer was started.
  100.  */
  101.  
  102. float
  103. timer_elapsed (t)
  104.     TIMER t;
  105. {
  106.     float diff;
  107.     
  108.     if (t->activated)
  109.         t->end = TickCount ();
  110.     
  111.     diff = t->end - t->start;
  112.     if (diff != 0)
  113.         return (diff / 60.0);
  114.     else
  115.         return 1.0;
  116. }
  117.